﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data;

public partial class pim_PersonalInfo : System.Web.UI.Page
{
    public ArrayList contactarr = null;
    public ArrayList emergencyarr = null;
    public ArrayList expedientearr = null;
    public ArrayList documentarr = null;
    public int per_id = 0;
    public int emp_id = 0;
    public int cont; 


    int tabid;
    public bool enable = false;
    protected void Page_Load(object sender, EventArgs e)
    {
        int tabindex = 0;
        tabid = -1;
        if (Request.QueryString["per_id"] != null) per_id = int.Parse(Request.QueryString["per_id"].ToString());
        
        if (per_id == 0) { Button1.Enabled = false;  } else { Button1.Enabled = true; }
        if (!Page.IsPostBack)
        {
            llenocompanias();
            if (Session["company"] != null) { llenobancos(int.Parse(Session["company"].ToString())); }
            llenocuentas();
            llenopuesto();
            llenocontrato();
            llenocountries();
            llenoanios(1950,2025);
            habilitocampos(false);
            enableDocuments(false);
            documentarr = DB.getArrDocument("");
            cb_typepopup.DataBind();
            ddl_newdoc.DataBind();

            if (Request.QueryString["per_id"] != null) { 
                per_id = int.Parse(Request.QueryString["per_id"].ToString());
                
                bt_save_affiliation.Visible = false;
                bt_save_emergency.Visible = false;
                bt_save_job.Visible = false;
                bt_save_personal.Visible = false;
                bt_save_salary.Visible = false;
                bt_save_doc.Visible = false;

                bt_edit_personal.Visible = false;
                bt_edit_affiliation.Visible = false;
                bt_edit_emer.Visible = false;
                bt_edit_job.Visible = false;
                bt_edit_salary.Visible = false;
                bt_edit_doc.Visible = false;
                

                bt_cancel_personal.Visible = false;
                bt_cancel_affi.Visible = false;
                bt_cancel_emergency.Visible = false;
                bt_cancel_job.Visible = false;
                bt_cancel_salary.Visible = false;
                bt_cancel_doc.Visible = false;

                

                bt_edit.Visible = true;
                bt_save.Visible = false;
               // bt_cancel.Visible = false;
                ln_back.Visible = true;
                tabid = -1;
                Session["tabid"] = tabid;
                lb_tabid.Text = "-1";
            
            }
            else {
                tabid = 0;
                lb_tabid.Text = "0";
                Session["tabid"] = tabid;
                habilitocampos(true);
                bt_edit_personal.Visible = false;
                bt_edit_personal.Visible = false;
                bt_edit_salary.Visible = false;
                bt_edit_job.Visible = false;
                bt_edit_personal.Visible = false;
                bt_edit_emer.Visible = false;
                
                int t = 1;
                if (Session["country"] != null)
                {

                    t = (int)Session["country"];
                    ddl_country.SelectedValue = t.ToString();
                    personalizarFormulario(t);
                    
                    if (t == 1) { ddl_moneda.SelectedValue = "GTQ"; }
                    if (t == 2) { ddl_moneda.SelectedValue = "USD"; }
                    if (t == 3) { ddl_moneda.SelectedValue = "USD"; }

                    if (Session["company"] != null) { ddl_company.SelectedValue = Session["company"].ToString(); }
                    if (Session["account"] != null) { ddl_cuenta.SelectedValue = Session["account"].ToString(); }
                    personalizarLogo(int.Parse(Session["company"].ToString()));
                    llenobancos(int.Parse(Session["company"].ToString()));
                }
                else {
                    
                    LinkButton4_ModalPopupExtender.Show();
                }

             
               
                
         
            }
            Session["per_id"] = per_id;
            
            if (Request.QueryString["tabindex"] != null) tabindex = int.Parse(Request.QueryString["tabindex"].ToString());

            TabContainer1.ActiveTabIndex = tabindex;

            RE_GenericBean personainf = DB.getPersonaInf(per_id, emp_id);
            
            lb_person_id.Text = personainf.intC1.ToString();
            lb_emp_id.Text = personainf.intC2.ToString();
            tb_nombre.Text = personainf.strC1.ToUpper();
            tb_nombre2.Text = personainf.strC2.ToUpper();
            tb_apellido.Text = personainf.strC3.ToUpper();
            tb_apellido2.Text = personainf.strC4.ToUpper();
            tb_apellidocasada.Text = personainf.strC5.ToUpper();
            lb_titulo_nombre.Text = personainf.strC1.ToUpper() + " " + personainf.strC2.ToUpper() + " " + personainf.strC3.ToUpper() + " " + personainf.strC4.ToUpper();
            tb_nit.Text = personainf.strC9.ToUpper();
            ddl_cuenta.SelectedValue = personainf.intC3.ToString();
            if (!ddl_cuenta.SelectedValue.Equals("0"))
            {
                ddl_cuenta_SelectedIndexChanged(sender, e);
                ddl_genero.SelectedValue = personainf.strC6;
                ddl_inmediate_superior.SelectedValue = personainf.intC6.ToString();
            }
            ddl_estadocivil.SelectedValue = personainf.strC7.ToUpper();
            try
            {
                DateTime time = DateTime.Parse(personainf.strC8);
                ddl_anio.SelectedValue = time.Year.ToString();
                ddl_mes.SelectedValue = time.Month.ToString();
                ddl_dia.SelectedValue = time.Day.ToString(); 
            } catch(Exception ex){
                ddl_anio.SelectedValue = "0";
                ddl_mes.SelectedValue = "0";
                ddl_dia.SelectedValue = "0" ;
            
            }

            
            
            tb_igss.Text = personainf.strC10.ToUpper();
            tb_irtra.Text = personainf.strC16.ToUpper();
            tb_codigoempresa.Text = personainf.strC11.ToUpper();
            
            if (personainf.intC4 != 0)
            {
                personalizarFormulario(personainf.intC4);
                ddl_company.SelectedValue = personainf.intC4.ToString();
            }
            /*ddl_company_SelectedIndexChanged(sender, e);
            ddl_cuenta_SelectedIndexChanged(sender, e);*/
            personalizarLogo(personainf.intC4);
            ddl_cuenta.SelectedValue = personainf.intC3.ToString();
            if (!ddl_cuenta.SelectedValue.Equals("0"))
            {
                ddl_cuenta_SelectedIndexChanged(sender, e);
                ddl_inmediate_superior.SelectedValue = personainf.intC6.ToString();
            }
            personainf.strC11=  validarfecha(personainf.strC11);
            
            tb_hired_date.Text = personainf.strC12;
            personainf.strC13 = validarfecha(personainf.strC13);
            tb_end_date.Text = personainf.strC13;
            tb_id.Text = personainf.strC14;
           
            ddl_type_id.SelectedValue = personainf.intC5.ToString();


             ArrayList personabankacc= null;
             emp_id =  int.Parse(lb_emp_id.Text);
            if (emp_id != 0)
            {
                personabankacc = DB.getArrEmpleadoCuentaBanco(" where emp_accbank_empid=" + emp_id + "and emp_accbank_status=1");
            }
            contactarr = (ArrayList)DB.getContactArr("where tpd_tper_id=" + lb_person_id.Text);
            emergencyarr = (ArrayList)DB.getEmergencyArr("where tpem_tper_id=" + lb_person_id.Text);
            expedientearr = (ArrayList)DB.getExpedienteArr(int.Parse(lb_emp_id.Text));

            if (emergencyarr != null && emergencyarr.Count > 0)
            {
                int cont = 0;
                foreach (RE_GenericBean rg in emergencyarr)
                {
                    if (cont == 0)
                    {
                        lb_emergency_id_1.Text = rg.intC1.ToString();
                        tb_emename_1.Text = rg.strC1.ToUpper();
                        tb_emerelacion_1.Text = rg.strC2.ToUpper();
                      
                        tb_emetel_1.Text = rg.strC8;
                   
                        tb_emecel_1.Text = rg.strC9;
                    }
                    else {
                        lb_emergency_id_2.Text = rg.intC1.ToString();
                      
                        tb_emename_2.Text = rg.strC1.ToUpper();
                        tb_emerelacion_2.Text = rg.strC2.ToUpper();
                        tb_emetel_3.Text = rg.strC8;
                        tb_emecel_2.Text = rg.strC9;
                
                    
                    
                    }
                    cont++;
                }

            }


            if (contactarr != null && contactarr.Count > 0)
            {
                foreach (RE_GenericBean rg in contactarr)
                {
                    lb_contactid_1.Text = rg.intC1.ToString();
                    tb_add1.Text = rg.strC1.ToUpper();
                    tb_add2.Text = rg.strC2.ToUpper();
                    tb_city.Text = rg.strC3.ToUpper();
                    tb_state.Text = rg.strC4.ToUpper();
                    tb_zip.Text = rg.strC5.ToUpper();
                    ddl_country.SelectedValue = rg.strC6;
                    if (rg.strC6.Equals("")) { rg.strC6 = "0"; }
                    personalizarFormulario(int.Parse(rg.strC6));
                    tb_tel.Text = rg.strC7;
                    tb_celular.Text = rg.strC8;
                    tb_email.Text = rg.strC11.ToLower();
                    tb_personalemail.Text = rg.strC12.ToLower();

                }
            }

            if (expedientearr != null && expedientearr.Count > 0)
            {
                foreach (RE_GenericBean rg in expedientearr)
                {
                    
                    tb_base_salary.Text = rg.decC1.ToString();
                    tb_bonus.Text = rg.decC2.ToString();
                    tb_commision.Text = rg.decC3.ToString();
                    tb_other.Text = rg.decC4.ToString();
                   // tb_hired_date.Text = rg.strC1;
                    rg.strC3 = validarfecha(rg.strC3);
                    tb_start_contract.Text = rg.strC3;
                    rg.strC4 = validarfecha(rg.strC4);
                    tb_end_contract.Text = rg.strC4;
                    lb_expediente_id_1.Text = rg.intC1.ToString();
                    tb_several_amount.Text = rg.decC5.ToString();
                    
                    ddl_empoyement.SelectedValue = rg.strC2;
                    ddl_moneda.SelectedValue = rg.strC5;
                    ddl_jobtitle.SelectedValue = rg.strC1;
                    tb_saltotal.Text = (rg.decC1 + rg.decC2).ToString();
                    ddl_status.SelectedValue = rg.intC7.ToString();
                    if (rg.intC8 == 1) ch_parking.Checked = true;
                    tb_sevagree.Text = rg.decC6.ToString();
                }
            }

            if (personabankacc != null && personabankacc.Count > 0)
            {
                DataTable dt = utility.getDataTableEmpleadoBancoCuenta(personabankacc);
                gv_bank.DataSource = dt;
                gv_bank.DataBind();
            }
           
            ddl_type_id.SelectedValue = personainf.intC5.ToString();
            if (!lb_emp_id.Text.Equals("0"))
            {
                llenardocumentos();
                habilitocampos(enable);

               

            }
            validarTabs();
        }
        else {

            string temp = ddl_genero.SelectedIndex.ToString();
           /* if (per_id != null &&  Session["per_id"]!= null) { 
                per_id = (int)Session["per_id"];

            }*/
            if (!lb_person_id.Text.Equals("") || !lb_person_id.Text.Equals("0")) {
                per_id = int.Parse(lb_person_id.Text);
            }
            else { per_id = 0; }
            if (tabid != null && Session["tabid"] != null)
            {
                tabid = (int)Session["tabid"];

            }
            else {
                tabid  =  int.Parse(lb_tabid.Text);
                
            }
            showErrors(false, -1);
            if (!lb_emp_id.Text.Equals("0"))
            {
                llenardocumentos();
            }
        }

        
    }

    private void llenardocumentos()
    {
        int compid = 0;
        if (Session["company"] != null)
            compid = int.Parse(Session["company"].ToString());
        ArrayList arr2 =   DB.getArrDocumentbyEmp(compid, 2, int.Parse(lb_emp_id.Text));
        DataTable dt2 = utility.getDataTableDocuments(arr2);
        gv_documents.DataSource = dt2;
        gv_documents.DataBind();

        foreach (DataRow dataRow in dt2.Rows)
        {
            string temp1 = dataRow[0].ToString();
            string temp2 = dataRow[1].ToString();
            string temp3 = dataRow[2].ToString();
            string temp4 = dataRow[3].ToString();
            string temp5 = dataRow[4].ToString();

            if (dataRow[1].ToString().ToUpper().Equals("PHOTO"))
            {
                if (!dataRow[4].ToString().Equals(""))
                    photo.ImageUrl = "~/data/documents/" + dataRow[4].ToString();
            }
        }
    }

    private void llenocountries()
    {
        ArrayList companyarr = null;
        ListItem item = null;
        try
        {
            companyarr = (ArrayList)DB.getArrCountries("");
            if (companyarr != null && companyarr.Count > 0)
            {
                foreach (RE_GenericBean company in companyarr)
                {
                    item = new ListItem(company.strC1, company.intC1.ToString());

                    ddl_country.Items.Add(item);
                }
            }
        }
        catch (Exception err)
        {
            utility.log(err.StackTrace);
        }
    }

    private void llenoanios(int inicial, int final)
    {
        
        ListItem item = null;
        item = new ListItem("Select One", "0");
        ddl_anio.Items.Add(item);
        try
        {

            for (int i = inicial; i <= final; i++ )
            {
                item = new ListItem(i.ToString(), i.ToString());

                ddl_anio.Items.Add(item);
            }
            
        }
        catch (Exception err)
        {
            utility.log(err.StackTrace);
        }
    }

    private void personalizarFormulario(int countryid)
    {//permite personalizar el formulario segun la compania con la que se este trabajando

        
       
        //para NIT    
        RE_GenericBean control    = utility.getControl(countryid, "label", lb_nit.ID);
        if (control != null)
        {
            lb_nit.Text = control.strC1;
            
        }
        //para tipo ID
        DataTable control2 = utility.getControlDDL(countryid, "ddl", ddl_type_id.ID);
        if (control2 != null)
        {
            ddl_type_id.DataSource = control2;
            ddl_type_id.DataTextField = "option";
            ddl_type_id.DataValueField = "id";
            ddl_type_id.DataBind();
        }

        //Para zipcode-zone
        RE_GenericBean control3 = utility.getControl(countryid, "label", lb_zipcode.ID);
        if (control3 != null)
        {
            lb_zipcode.Text = control3.strC1;

        }

        //Para validar exp zipcode-zone
        RE_GenericBean control4 = utility.getControl(countryid, "exp_regular", rg_zip.ID);
        if (control4 != null)
        {
             rg_zip.ErrorMessage=  control4.strC1;
            //utility.log("Validacion en Exp regular :"+ control4.strC2+" Mensaje de Error "+control4.strC1);
             rg_zip.ValidationExpression = control4.strC2;

        }
        //Para State-Depto
        RE_GenericBean control5 = utility.getControl(countryid, "label", lb_state.ID);
        if (control5 != null)
        {
            lb_state.Text = control5.strC1;

        }
        RE_GenericBean control6 = utility.getControl(countryid, "textbox", tb_state.ID);
        if (control6 != null)
        {
            tb_state.MaxLength = control6.intC1;

        }

       
       

    }
    private void personalizarLogo(int companyid)
    {//permite personalizar el formulario segun la compania con la que se este trabajando

        RE_GenericBean company = DB.getCompania(companyid);
        if (!company.strC5.Equals("")) { logo.ImageUrl = company.strC15; }


        
    }

    private string validarfecha(string fecha)
    {
        string result= fecha;
        if (fecha.Equals("1/1/1900") || fecha.Equals("01/01/1900")) {
            result = "";
        }
        return result;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Guardar(1);
    }

    private int Guardar(int pestana) {
        int res = 1;
    
        if (pestana==1 ){

            //lb_error.Visible = false;
            showErrors(false, 1);

            if (validar(1))
            {


                RE_GenericBean personalinf = new RE_GenericBean();
                bool actualiza = false;

                personalinf.intC1 = int.Parse(lb_person_id.Text);
                personalinf.intC2 = int.Parse(lb_emp_id.Text);
                personalinf.strC1 = tb_id.Text.Trim().ToUpper();//id personal (dpi, cedula )
                personalinf.strC2 = tb_nombre.Text.Trim().ToUpper();
                personalinf.strC3 = tb_nombre2.Text.Trim().ToUpper();
                personalinf.strC4 = tb_apellido.Text.Trim().ToUpper();
                personalinf.strC5 = tb_apellido2.Text.Trim().ToUpper();
                personalinf.strC6 = tb_apellidocasada.Text.Trim().ToUpper();
                personalinf.strC7 = ddl_genero.SelectedValue.ToUpper();
                personalinf.strC8 = ddl_estadocivil.SelectedValue.ToUpper();
                personalinf.strC9 = ddl_mes.SelectedValue + "/" + ddl_dia.SelectedValue + "/" + ddl_anio.SelectedValue;

                personalinf.intC3 = int.Parse(ddl_cuenta.SelectedValue.ToString());


                RE_GenericBean contact1 = null;
                if (!tb_add1.Text.Equals(""))
                {
                    contact1 = new RE_GenericBean();
                    contact1.intC1 = int.Parse(lb_contactid_1.Text);
                    contact1.strC1 = tb_add1.Text.ToUpper();
                    contact1.strC2 = tb_add2.Text.ToUpper();
                    contact1.strC3 = tb_city.Text.ToUpper();
                    contact1.strC4 = tb_state.Text.ToUpper();
                    contact1.strC5 = tb_zip.Text.ToUpper();
                    contact1.strC6 = ddl_country.SelectedValue;
                    contact1.strC7 = tb_tel.Text;
                    contact1.strC8 = tb_celular.Text;
                    contact1.strC9 = tb_email.Text.ToLower();
                    contact1.strC10 = tb_personalemail.Text.ToLower();
                }




                if (personalinf.intC1 != 0) actualiza = true;

                //RE_GenericBean result = DB.InsertaPersonalInfo(personalinf, contact1, contact2, emergency1, emergency2, jobposition, salary, actualiza);
                RE_GenericBean result = DB.InsertaPersonalInfoPer(personalinf, contact1, actualiza);
                if (result != null)
                { // si es diferente de null es porque si se pudo insertar
                    //lb_person_id.Text = result.intC1.ToString();
                  //  per_id = result.intC1;
                   // lb_emp_id.Text = result.intC3.ToString();
                  //  emp_id = result.intC3;
                    tabid = int.Parse(lb_tabid.Text);
                    if (tabid != -1)
                    {
                        bt_edit_personal.Visible = false;
                        bt_edit_affiliation.Visible = false;
                        bt_edit_emer.Visible = false;
                        bt_edit_salary.Visible = false;
                        bt_edit_doc.Visible = false;

                        bt_save_personal.Visible = true;
                        bt_save_affiliation.Visible = true;
                        bt_save_emergency.Visible = true;
                        bt_save_salary.Visible = true;
                        bt_save_doc.Visible = true;
                        tabid = 1;
                        lb_tabid.Text = "1";
                        if (!lb_person_id.Text.Trim().Equals("0")) { TabContainer1.ActiveTabIndex = 1; }
                        res = res*1;
                    }

                    lb_error_personal.Text = "Info Saved!!";
                    lb_error_personal.Style.Add("color", "blue");
                    lb_error_personal.Visible = true;
                    res = res * 1;
                    validarTabs();
                    
                }
            }
            else
            {

                lb_error_personal.Text = "Info not saved ";
                lb_error_personal.Style.Add("color", "Red");
                lb_error_personal.Visible = true;
                bt_edit_personal.Visible = false;
                bt_save.Visible = true;
                habilitocampos(true);
                res = res * 0;
            }
            
        
        
        }
        //si es pestana 2
        else if (pestana == 2 )
        
        {
           // lb_error.Visible = false;
            showErrors(false, 2);
            if (validar(2))
            {
                bool actualiza = false;
                RE_GenericBean affilliation = new RE_GenericBean();
                affilliation.strC6 = lb_person_id.Text;
                affilliation.strC1 = tb_id.Text.ToUpper();
                affilliation.strC2 = tb_nit.Text.ToUpper();
                affilliation.strC3 = tb_igss.Text.ToUpper();
                affilliation.strC4 = tb_irtra.Text.ToUpper();
                affilliation.strC5 = ddl_type_id.SelectedValue;
                if (affilliation.intC1 != 0) actualiza = true;
                if (DB.UpdateAffiliationInfo(affilliation, actualiza))
                {
                    lb_error_contact.Text = "Info Saved";
                    lb_error_contact.Style.Add("color", "blue");
                    lb_error_contact.Visible = true;
                    tabid = int.Parse(lb_tabid.Text);
                    if (tabid != -1)
                    {
                        bt_edit_personal.Visible = false;
                        bt_edit_affiliation.Visible = false;
                        bt_edit_emer.Visible = false;
                        bt_edit_salary.Visible = false;
                        bt_edit_doc.Visible = false;

                        bt_save_personal.Visible = true;
                        bt_save_affiliation.Visible = true;
                        bt_save_emergency.Visible = true;
                        bt_save_salary.Visible = true;
                        bt_save_doc.Visible = true;

                        tabid = 2;
                        lb_tabid.Text = "2";
                        if (!lb_person_id.Text.Trim().Equals("0")) { TabContainer1.ActiveTabIndex = 2; }
                    }
                    validarTabs();
                    res = res * 1;
                }
                else
                {
                    lb_error_contact.Text = "Info not saved";
                    lb_error_contact.Style.Add("color", "Red");
                    lb_error_contact.Visible = true;
                    res = res * 0;
                }

            }

            else
            {
                lb_error_contact.Text = " Missing one or more Items ";
                lb_error_contact.Style.Add("color", "Red");
                lb_error_contact.Visible = true;
                res = res * 0;

            }
        
        
        }
        else if (pestana == 3 )
        {//emergency

            // lb_error.Visible = false;
        showErrors(false, 3);
        if (validar(3))
        {
            RE_GenericBean emergency1 = null;
            if (!tb_emename_1.Text.Equals(""))
            {
                emergency1 = new RE_GenericBean();
                emergency1.intC1 = int.Parse(lb_emergency_id_1.Text);
                emergency1.strC1 = tb_emename_1.Text.ToUpper();
                emergency1.strC2 = tb_emerelacion_1.Text.ToUpper();
                emergency1.strC9 = tb_emetel_1.Text;
                emergency1.strC11 = tb_emecel_1.Text;
                emergency1.intC2 = int.Parse(lb_person_id.Text.Trim());
             
            }

            RE_GenericBean emergency2 = null;
            if (!tb_emename_2.Text.Equals(""))
            {
                emergency2 = new RE_GenericBean();
                emergency2.intC1 = int.Parse(lb_emergency_id_2.Text);
             
                emergency2.strC1 = tb_emename_2.Text.Trim().ToUpper();
                emergency2.strC2 = tb_emerelacion_2.Text.Trim().ToUpper();
                emergency2.strC9 = tb_emetel_3.Text.Trim();
                emergency2.strC11 = tb_emecel_2.Text.Trim();
                emergency2.intC2 = int.Parse(lb_person_id.Text.Trim());
            }
            bool actualiza = false;
            bool actualiza2 = false;
            RE_GenericBean res3 = new RE_GenericBean();
            RE_GenericBean res2 = new RE_GenericBean();
            res2.boolC1 = true;
            if (emergency1 != null && emergency1.intC1 != 0) actualiza = true;
            if (emergency2 != null && emergency2.intC1 != 0) actualiza2 = true;

            if (emergency1 != null)
            {
                res3 = DB.InsertaEmergencyInfo(emergency1, actualiza);
            }
            if (emergency2 != null)
            {

                res2 = DB.InsertaEmergencyInfo(emergency2, actualiza2);
            }
            if (res3.boolC1 && res2.boolC1)
            {
                lb_emergency_id_1.Text = res3.intC1.ToString();
                lb_emergency_id_2.Text = res2.intC1.ToString();


                
                lb_error_emergency.Text = "Emergency Info  Saved";
                lb_error_emergency.Style.Add("color", "blue");
                lb_error_emergency.Visible = true;
                tabid = int.Parse(lb_tabid.Text);
                if (tabid != -1)
                {
                    bt_edit_personal.Visible = false;
                    bt_edit_affiliation.Visible = false;
                    bt_edit_emer.Visible = false;
                    bt_edit_salary.Visible = false;
                    bt_edit_doc.Visible = false;

                    bt_save_personal.Visible = true;
                    bt_save_affiliation.Visible = true;
                    bt_save_emergency.Visible = true;
                    bt_save_salary.Visible = true;
                    bt_save_doc.Visible = true;
                    lb_tabid.Text = "3";
                    tabid = 3;
                    if (!lb_emergency_id_1.Text.Trim().Equals("0"))
                    {
                        TabContainer1.ActiveTabIndex = 3;

                    }
                }
                validarTabs();
                habilitocampos(true);
                res = res * 1;
                if (tabid != -1)
                {
                    bt_save.Visible = false;
                }

            }

        }
        else
        {
            lb_error_emergency.Text = " Emergency Info not saved";
            lb_error_emergency.Style.Add("color", "Red");
            lb_error_emergency.Visible = true;
           
            res = res * 1;
        }



        }
        else if (pestana == 4 )
        { //jobposition

           // lb_error.Visible = false;
            showErrors(false, 4);
            if (validar(4))
            {

                RE_GenericBean jobposition = new RE_GenericBean();
                if (tb_hired_date.Text != null && !tb_hired_date.Text.Equals(""))
                {
                    jobposition.intC1 = int.Parse(ddl_jobtitle.SelectedValue);
                    jobposition.intC2 = int.Parse(ddl_status.SelectedValue);
                    if (jobposition.strC1.Equals("1/1/1900")) jobposition.strC1 = "";
                    jobposition.strC1 = tb_hired_date.Text;
                    if (jobposition.strC2.Equals("1/1/1900")) jobposition.strC2 = "";
                    jobposition.strC2 = tb_end_date.Text;
                    if (jobposition.strC3.Equals("1/1/1900")) jobposition.strC3 = "";
                    jobposition.strC3 = tb_start_contract.Text;
                    if (jobposition.strC4.Equals("1/1/1900")) jobposition.strC4 = "";
                    jobposition.strC4 = tb_end_contract.Text;
                    jobposition.strC5 = ddl_inmediate_superior.SelectedValue;
                    jobposition.intC3 = int.Parse(ddl_company.SelectedValue);
                    jobposition.intC4 = int.Parse(ddl_cuenta.SelectedValue);
                    jobposition.strC6 = tb_codigoempresa.Text.Trim().ToUpper();
                    jobposition.intC5 = int.Parse(lb_person_id.Text.Trim());
                    jobposition.intC6 = int.Parse(ddl_status.SelectedValue.ToString());
                    jobposition.intC7 = int.Parse(lb_emp_id.Text.Trim());//id de empleado
                    jobposition.intC8 = int.Parse(ddl_inmediate_superior.SelectedValue);//id del jefe
                    bool actualiza = false;
                    if (!lb_emp_id.Text.Trim().Equals("0"))
                    {
                        RE_GenericBean res3 = DB.InsertJobPositionEmp(jobposition, actualiza);
                        if (res3.boolC1)
                        {
                            lb_emp_id.Text = res3.intC7.ToString();
                            lb_error_job.Text = " Info saved";
                            lb_error_job.Style.Add("color", "blue");
                            lb_error_job.Visible = true;
                            tabid = int.Parse(lb_tabid.Text);
                            if (tabid != -1)
                            {
                                bt_edit_personal.Visible = false;
                                bt_edit_affiliation.Visible = false;
                                bt_edit_emer.Visible = false;
                                bt_edit_salary.Visible = false;
                                bt_edit_doc.Visible = false;

                                bt_save_personal.Visible = true;
                                bt_save_affiliation.Visible = true;
                                bt_save_emergency.Visible = true;
                                bt_save_salary.Visible = true;
                                bt_save_doc.Visible = true;

                                habilitocampos(true);
                                res = res * 1;
                                lb_tabid.Text = "4";
                                tabid = 4;
                                if (!lb_emp_id.Text.Trim().Equals("0")) { TabContainer1.ActiveTabIndex = 4; }
                            }
                            habilitocampos(true);
                            validarTabs();
                            if (tabid != -1)
                            {
                                bt_save.Visible = false;
                            }

                        }
                        else
                        {

                            lb_error_job.Text = "Info not saved";
                            lb_error_job.Style.Add("color", "Red");
                            lb_error_job.Visible = true;
                            res = res * 0;
                        }

                    }
                    else {
                        lb_error_job.Text = "Info not saved Emp_id lost";
                        lb_error_job.Style.Add("color", "Red");
                        lb_error_job.Visible = true;
                        res = res * 0;
                    
                    }

                }

            }
            else
            {
                lb_error_job.Text = " Missing one or more items";
                lb_error_job.Style.Add("color", "Red");
                lb_error_job.Visible = true;
                res = res * 0;

            }



        }
        else if (pestana == 5 )
        {//salary

            //lb_error.Visible = false;
            showErrors(false, 5);
            if (validar(5))
            {

                RE_GenericBean salary = new RE_GenericBean();
                if (tb_base_salary.Text != null && !tb_base_salary.Text.Equals(""))
                {
                    salary.decC1 = decimal.Parse(tb_base_salary.Text);
                    salary.decC2 = decimal.Parse(tb_bonus.Text);
                    salary.decC3 = decimal.Parse(tb_commision.Text);
                    salary.decC4 = decimal.Parse(tb_other.Text);
                    salary.decC5 = decimal.Parse(tb_several_amount.Text);
                    salary.strC1 = ddl_moneda.SelectedValue;
                    salary.intC1 = int.Parse(lb_person_id.Text.Trim());//personaid
                    salary.intC4 = int.Parse(ddl_cuenta.SelectedValue); //cuenta
                    salary.intC2 = int.Parse(ddl_empoyement.SelectedValue);//ttcontrato
                    salary.intC5 = int.Parse(lb_emp_id.Text);//emp_id
                    if (ch_parking.Checked) salary.intC6 = 1;
                    salary.decC6 = decimal.Parse(tb_sevagree.Text);
                    tb_saltotal.Text = (decimal.Parse(tb_base_salary.Text) + decimal.Parse(tb_bonus.Text)).ToString();


                    if (DB.InsertSalaryEmp(salary))
                    {
                        tabid= int.Parse(lb_tabid.Text);
                        if (tabid != -1)
                        {
                            bt_edit_personal.Visible = false;
                            bt_edit_affiliation.Visible = false;
                            bt_edit_emer.Visible = false;
                            bt_edit_salary.Visible = false;
                            bt_edit_doc.Visible = false;

                            bt_save_personal.Visible = true;
                            bt_save_affiliation.Visible = true;
                            bt_save_emergency.Visible = true;
                            bt_save_salary.Visible = true;
                            bt_save_doc.Visible = true;
                            bt_save_doc.Enabled = true;
                            bt_cancel_doc.Visible = false;
                            TabPanel6.Enabled = true;
                            tabid = 5;
                            lb_tabid.Text = "5";
                            Session["tabid"] = tabid;
                            TabContainer1.ActiveTabIndex = 5;
                            llenardocumentos();
                            
                           
                        }
                        lb_error_salary.Text = "Info Saved";
                        lb_error_salary.Style.Add("color", "blue");
                        lb_error_salary.Visible = true;
                        res = res * 1;
                         
                        habilitocampos(true);
                        validarTabs();

                    }
                    else
                    {

                        lb_error_salary.Text = "Info not saved";
                        lb_error_salary.Style.Add("color", "Red");
                        lb_error_salary.Visible = true;
                        res = res * 0;
                    }
                }

            }
            else
            {
                lb_error_salary.Text = " Missing One or more items";
                lb_error_salary.Style.Add("color", "Red");
                lb_error_salary.Visible = true;
                res = res * 0;


            }
        
        }
        return res;
    
   
    }
    

    private void validarTabs()
    {
        //primero las habilito todas
        alert.Visible = false;
        TabPanel2.Enabled = true; TabPanel3.Enabled = true; TabPanel4.Enabled = true; TabPanel5.Enabled = true; TabPanel6.Enabled = true;
        TabPanel2.Visible = true; TabPanel3.Visible = true; TabPanel4.Visible = true; TabPanel5.Visible = true; TabPanel6.Visible = true;
        tab_personal.Visible = false; tab_afi.Visible = false; tab_emer.Visible = false; tab_job.Visible = false; tab_salary.Visible = false; tab_doc.Visible = false;
      //deshabilito las pestanas si no se cumplen las validaciones

        if (tabid == 0) { TabPanel2.Enabled = false; TabPanel2.Visible = false; TabPanel3.Enabled = false; TabPanel3.Visible = false; TabPanel4.Enabled = false; TabPanel4.Visible = false; TabPanel5.Enabled = false; TabPanel5.Visible = false; TabPanel6.Enabled = false; TabPanel6.Visible = false; }
        if (tabid == 1) { TabPanel3.Enabled = false; TabPanel3.Visible = false; TabPanel4.Enabled = false; TabPanel4.Visible = false; TabPanel5.Enabled = false; TabPanel5.Visible = false; TabPanel6.Enabled = false; TabPanel6.Visible = false; }
        if (tabid == 2) { TabPanel4.Enabled = false; TabPanel4.Visible = false; TabPanel5.Enabled = false; TabPanel5.Visible = false; TabPanel6.Enabled = false; TabPanel6.Visible = false; }
        if (tabid == 3) { TabPanel5.Enabled = false; TabPanel5.Visible = false; TabPanel6.Enabled = false; TabPanel6.Visible = false; }
        if (tabid == 4) { TabPanel6.Enabled = false; TabPanel6.Visible = false; tabid = 4;  }
        //if (tabid == 5) { TabPanel6.Enabled = false; }


        if (tabid == -1)
        {
            int sg = 1;
            //TabPanel2.Enabled = true; TabPanel3.Enabled = true; TabPanel4.Enabled = true; TabPanel5.Enabled = true; 
            if (tb_id.Text.Equals("") && lb_emergency_id_1.Text.Trim().Equals("0")) { TabPanel3.Enabled = false; TabPanel4.Enabled = false; TabPanel5.Enabled = false; TabPanel6.Enabled = false; sg = sg * 0; }
            if (!tb_id.Text.Equals("") && lb_emergency_id_1.Text.Trim().Equals("0")) { TabPanel4.Enabled = false; TabPanel5.Enabled = false; TabPanel6.Enabled = false; sg = sg * 0; }
            if (!tb_id.Text.Equals("") && !lb_emergency_id_1.Text.Trim().Equals("0") && lb_emp_id.Text.Trim().Equals("0")) { TabPanel5.Enabled = false; TabPanel6.Enabled = false; sg = sg * 0; }
            if (!tb_id.Text.Equals("") && !lb_emergency_id_1.Text.Trim().Equals("0") && !lb_emp_id.Text.Trim().Equals("0") && tb_base_salary.Text.Equals("0.00")) { TabPanel6.Enabled = false; sg = sg * 0; }

            if (sg == 1)
            {

                bt_save_personal.Visible = false; bt_save_affiliation.Visible = false; bt_save_emergency.Visible = false; bt_save_job.Visible = false; bt_save_salary.Visible = false; bt_save_doc.Visible = false; 
                bt_cancel_personal.Visible = false; bt_cancel_affi.Visible = false; bt_cancel_emergency.Visible = false; bt_cancel_job.Visible = false; bt_cancel_salary.Visible = false; bt_cancel_doc.Visible = false;


            }

            /*   if (lb_person_id.Text.Trim().Equals("0")) { TabPanel2.Enabled = false; TabPanel3.Enabled = false; TabPanel4.Enabled = false; TabPanel5.Enabled = false; }
               else 
               else if (lb_emp_id.Text.Trim().Equals("0")) { TabPanel5.Enabled = false; }

               */
            lb_tabid.Text = tabid.ToString();
            string temp = "<label class='tip'>&nbsp;";
            alert.Visible = false; 
            //ahora pinto colores si no estan completas
            //para TAB1
            if ( tb_apellido2.Text.Equals("") || ddl_anio.SelectedIndex == 0 || ddl_mes.SelectedIndex == 0 || ddl_dia.SelectedIndex == 0 || tb_add1.Text.Equals("") || tb_city.Text.Equals("") || tb_state.Text.Equals("") || tb_zip.Text.Equals("") || ddl_country.SelectedIndex == 0 || tb_tel.Text.Equals("") || tb_celular.Text.Equals("") || tb_email.Text.Equals("") || tb_personalemail.Text.Equals("") || ddl_country.SelectedIndex==0)
            {
                //alert.Visible = true;
                // if (!temp.Equals("")) temp += ",";
                //temp += "<label class='tip'>&nbsp; Personal</label> ";
                temp += " Personal";
                tab_personal.Visible = true;
                //alert_1.Text = temp;
            }


            //para TAB2
            if (ddl_type_id.SelectedIndex == 0 || tb_nit.Text.Equals("") || tb_igss.Text.Equals("") || tb_irtra.Text.Equals(""))
            {

                //alert_1.Visible = true;
                if (!temp.Equals("<label class='tip'>&nbsp;")) temp += ",";
                //temp += "<label class='tip'>&nbsp; Affilliation</label> ";
                temp += "&nbsp; Affilliation";
                tab_afi.Visible = true;
                //alert_1.Text = temp;

            }
            //para TAB3(emergency)
            if (tb_emename_1.Text.Equals("") || tb_emename_2.Text.Equals("") || tb_emerelacion_1.Text.Equals("") || tb_emerelacion_2.Text.Equals("") || tb_emetel_1.Text.Equals("") || tb_emetel_3.Text.Equals("") || tb_emecel_1.Text.Equals("") || tb_emecel_2.Text.Equals(""))
            {
                //alert_2.Visible = true;
                if (!temp.Equals("<label class='tip'>&nbsp;")) temp += ",";
               // temp = " <label class='tip'>&nbsp;  Emergency </label>  ";
                temp += "&nbsp; Emergency";
                //alert_2.Text = temp;
                tab_emer.Visible = true;

            }
            //TAB 4 Jobposition
            if (ddl_company.SelectedIndex == 0 || ddl_cuenta.SelectedIndex == 0 || ddl_jobtitle.SelectedIndex == 0 || tb_hired_date.Text.Equals("") || tb_start_contract.Text.Equals("")  || tb_codigoempresa.Text.Equals(""))
            {
            
                //alert_3.Visible = true;
                if (!temp.Equals("<label class='tip'>&nbsp;")) temp += ",";
                //temp = " <label class='tip'>&nbsp;Job Position</label>  ";
                temp += "&nbsp; Job Position";
                //alert_3.Text = temp;
                tab_job.Visible = true;
            
            }
            // TAB5 salary
            if (tb_base_salary.Text.Equals("") || tb_bonus.Text.Equals("") || tb_commision.Text.Equals("") || tb_other.Text.Equals("") || tb_several_amount.Text.Equals("") || ddl_moneda.SelectedIndex==0 || ddl_empoyement.SelectedIndex==0)
            {
            
                //alert_4.Visible = true;
                if (!temp.Equals("<label class='tip'>&nbsp;")) temp += ",";
                //temp = " <label class='tip'>&nbsp;  Salary </label>  ";
                temp += "&nbsp;Salary";
                //alert_4.Text = temp;
                tab_salary.Visible = true;
            
            }
            
            RE_GenericBean  valdoc= validardocs();
            if (!valdoc.boolC1) {
                if (!temp.Equals("<label class='tip'>&nbsp;")) temp += ",";
                temp += "&nbsp; Documents";
                //alert_5.Visible = true;
                tab_doc.Visible = true;
            
            }
         
         /*  if (!temp.Equals("<label class='tip'>&nbsp;"))  alert.Visible= true;
               alert.Text = temp + "</label>"; ;*/
              
        }
    }

    private RE_GenericBean validardocs()
    {
        RE_GenericBean result = new RE_GenericBean();
        if (gv_documents != null && gv_documents.Rows.Count > 0)
        {
            int totalobligatorios = 0;
            int cont = 0;
            result.boolC1 = true;

            foreach (GridViewRow ROW in gv_documents.Rows)
            {
               
                    int con = ROW.Controls.Count;

                    Label lbid = (Label)ROW.FindControl("lb_iddocemp");
                    Label lpath = (Label)ROW.FindControl("lb_path");
                    Label lobligatorio = (Label)ROW.FindControl("lb_obligatorio");
                    Label lbidtipodoc = (Label)ROW.FindControl("lb_iddoc");
                    int id = int.Parse(lbid.Text);
                    if (lobligatorio.Text.Equals("*"))
                    {
                        totalobligatorios++;
                        if (lpath.Text.Equals("") )
                        {
                            result.boolC1 = result.boolC1 & false;
                            result.strC1 += lbidtipodoc.Text;

                        }
                    }
                    
                
            }
            
        }
        //lb_error.Text = result.strC1;
        //lb_error.Visible = true;
        return result;
    }

    private void showErrors(bool p, int pestana)
    {
        if (pestana == 1 || pestana==-1)//personal
        {

            lb_marital_error.Visible = p;
           // lb_error.Visible = p;
            
            lb_nombre_error.Visible = p;
            lb_peremail_error.Visible = p;
           
            lb_hometel_error.Visible = p;
            lb_gender_error.Visible = p;
            
            lb_cel_error.Visible = p;
            lb_apellido_error.Visible = p;

            lb_error_personal.Visible = p;
            lb_fecha_error.Visible = p;
          


        }
        if (pestana == 2 || pestana == -1)//afilliation
        {
            lb_id_error.Visible = p;
            lb_error_contact.Visible = p;

        }
        if (pestana == 3 || pestana == -1)//emergency
        {
            
             lb_releme_error.Visible = p;
            lb_teleme_error.Visible = p;
            lb_nameemer_error.Visible = p;
            lb_nameemer_error.Visible = p;
            lb_error_emergency.Visible = p;

        }
        if (pestana == 4 || pestana == -1)//jobposition
        {
            lb_jobtitle_error.Visible = p;
            lb_acc_error.Visible = p;
            lb_hired_error.Visible = p;
               
            lb_comp_error.Visible = p;
            lb_error_job.Visible = p;
            lb_batch.Visible = p;
        }
        if (pestana == 5 || pestana == -1)//salary
        {

            lb_currency_error.Visible = p;
            lb_emplotype_error.Visible = p;
            lb_error_salary.Visible = p;
            

        }
        lb_error_doc.Visible = p;
       // lb_error.Visible = p;
    }

    private bool validar(int pestana)
    {
        bool result;
        int res = 1;

        //primero valuo combobox ninguno con opcion select one
        if (pestana == 1) { //personal
            if (ddl_genero.SelectedIndex == 0) { res = res * 0; lb_gender_error.Visible = true; }
            if (ddl_estadocivil.SelectedIndex == 0) { res = res * 0; lb_marital_error.Visible = true; }
            if (tb_nombre.Text.Equals("")) { res = res * 0; lb_nombre_error.Visible = true; }
            if (tb_apellido.Text.Equals("")) { res = res * 0; lb_apellido_error.Visible = true; }
            if (tb_tel.Text.Equals("")) { res = res * 0; lb_hometel_error.Visible = true; }
            if (tb_celular.Text.Equals("")) { res = res * 0; lb_cel_error.Visible = true; }
            if (tb_personalemail.Text.Equals("")) { res = res * 0; lb_peremail_error.Visible = true; }

            if (ddl_genero.SelectedValue.Equals("F") && ddl_estadocivil.SelectedValue.Equals("MARRIED")) {
                if (tb_apellidocasada.Text.Equals("")) { res = res * 0; lb_apellidocasada_error.Visible = true; }
            }
            if (ddl_mes.SelectedValue == "0" || ddl_anio.SelectedValue == "0" || ddl_dia.SelectedValue=="0") {
                res = res * 0; lb_fecha_error.Visible = true;
            }

        }
        if (pestana == 2)
        { //affilliate
            if (tb_id.Text.Equals("")) { res = res * 0; lb_id_error.Visible = true; }

        }
        if (pestana == 3)
        { //emergency
            if (tb_emename_1.Text.Equals("")) { res = res * 0; lb_nameemer_error.Visible = true; }
            if (tb_emerelacion_1.Text.Equals("")) { res = res * 0; lb_releme_error.Visible = true; }
            if (tb_emetel_1.Text.Equals("")) { res = res * 0; lb_teleme_error.Visible = true; }


        }

        if (pestana == 4)
        { //jobposition
            if (ddl_jobtitle.SelectedIndex == 0) { res = res * 0; lb_jobtitle_error.Visible = true; }
            if (ddl_company.SelectedIndex == 0) { res = res * 0; lb_jobtitle_error.Visible = true; }
            if (ddl_cuenta.SelectedIndex == 0) { res = res * 0; lb_jobtitle_error.Visible = true; }
            if (tb_hired_date.Text.Equals("")) { res = res * 0; lb_hired_error.Visible = true; }
            if (ddl_inmediate_superior.SelectedIndex==0) { res = res * 0; lb_inmediate_error.Visible = true; }
            emp_id = int.Parse(lb_emp_id.Text);
            int r = DB.existEmployeeCode(tb_codigoempresa.Text, emp_id);
            if (r != 0) { res = res * 0; lb_batch.Visible = true; }
        }

        if (pestana == 5)
        { //salary
            if (ddl_moneda.SelectedIndex == 0) { res = res * 0; lb_currency_error.Visible = true; }
            if (ddl_empoyement.SelectedIndex == 0) { res = res * 0; lb_emplotype_error.Visible = true; }
        }

        
      

        if (res == 0) { result = false;  }
        else result = true; 
        return result;
    }

    private void llenocompanias()
    {
        ArrayList companyarr = null;
        ListItem item = null;
        try
        {
            companyarr = (ArrayList)DB.getArrCompania("");
            if (companyarr != null && companyarr.Count > 0)
            {
                foreach (RE_GenericBean company in companyarr)
                {
                    item = new ListItem(company.strC1, company.intC1.ToString());
                    ddl_company.Items.Add(item);
                }
            }
        }
        catch (Exception err)
        {
            utility.log(err.StackTrace);
        }
    }
    private void llenobancos(int companyid) {
        ddl_bankpopup.Items.Clear();
        ArrayList companyarr = null;
        ListItem item = new ListItem();
        item.Value = "0";
        item.Text = "Select One";
        ddl_bankpopup.Items.Add(item);
        try
        {
            companyarr = (ArrayList)DB.getArrBancosbyCompany("where idcompany="+companyid);
            
            if (companyarr != null && companyarr.Count > 0)
            {
                foreach (RE_GenericBean company in companyarr)
                {
                    item = new ListItem(company.strC1, company.intC1.ToString());
                    
                    ddl_bankpopup.Items.Add(item);
                }
            }
        }
        catch (Exception err)
        {
            utility.log(err.StackTrace);
        }
    
    }
    private void llenocuentas()
    {
        ArrayList accarr = null;
        ListItem item = null;
        try
        {
            accarr = (ArrayList)DB.getArrCuenta("");
            if (accarr != null && accarr.Count > 0)
            {
                foreach (RE_GenericBean ac in accarr)
                {
                    item = new ListItem(ac.strC1, ac.intC1.ToString());
                    ddl_cuenta.Items.Add(item);
                }
            }
        }
        catch (Exception err)
        {
            utility.log(err.StackTrace);
        }
    }
    protected void ddl_company_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddl_company.SelectedIndex != 0 && ddl_company.SelectedIndex != -1)
        {
            ArrayList cuentasarr = null;
            ListItem item = null;
            ddl_cuenta.Items.Clear();
            item = new ListItem("Select One", "Select One");
            ddl_cuenta.Items.Add(item);
            
            try
            {
                cuentasarr = (ArrayList)DB.getArrCuenta("where tcu_tco_id=" + ddl_company.SelectedValue);
                if (cuentasarr != null && cuentasarr.Count > 0)
                {
                    foreach (RE_GenericBean cuenta in cuentasarr)
                    {
                        item = new ListItem(cuenta.strC1, cuenta.intC1.ToString());
                        ddl_cuenta.Items.Add(item);
                    }
                }
                //personalizarFormulario(int.Parse(ddl_company.SelectedValue));
                personalizarLogo(int.Parse(ddl_company.SelectedValue));
                ddl_inmediate_superior.Items.Clear();

            }
            catch (Exception err)
            {
                utility.log(err.StackTrace);
            }
        }
    }

    private void llenopuesto()
    {
        ArrayList puestoarr = null;
        ListItem item = null;
        try
        {
            puestoarr = (ArrayList)DB.getArrPuestoEmpleado("");
            if (puestoarr != null && puestoarr.Count > 0)
            {
                foreach (RE_GenericBean puesto in puestoarr)
                {
                    item = new ListItem(puesto.strC1, puesto.intC1.ToString());
                    ddl_jobtitle.Items.Add(item);
                }
            }
        }
        catch (Exception err)
        {
            utility.log(err.StackTrace);
        }
    }

    private void llenocontrato()
    {
        ArrayList contratoarr = null;
        ListItem item = null;
        try
        {
            contratoarr = (ArrayList)DB.getArrTipoContrato("");
            if (contratoarr != null && contratoarr.Count > 0)
            {
                foreach (RE_GenericBean contrato in contratoarr)
                {
                    item = new ListItem(contrato.strC1, contrato.intC1.ToString());
                    ddl_empoyement.Items.Add(item);
                }
            }
        }
        catch (Exception err)
        {
            utility.log(err.StackTrace);
        }
    }

    public DataTable getbancos()
    {
        ArrayList companyarr = null;
        DataTable dt= null;
        
        try
        {
            companyarr = (ArrayList)DB.getArrBancos("");
            if (companyarr != null && companyarr.Count > 0)
            {
                dt = utility.getDataTableBancos(companyarr);
            }
        }
        catch (Exception err)
        {
            utility.log(err.StackTrace);
        }
        return dt;
    }
    public string getDocumentSelectedValue(int cont) {

        string result = "0";

        result = cont.ToString();
        




        return result;

    
    }
    public DataTable getDocuments()
    {
        ArrayList companyarr = null;
        DataTable dt = null;

        try
        {
            int compid = 0;
            if (Session["company"] != null)
                compid = int.Parse(Session["company"].ToString());
            companyarr = (ArrayList)DB.getArrDocsPerComps(compid, 2);
            if (companyarr != null && companyarr.Count > 0)
            {
                dt = utility.getDataTableDocuments(companyarr);
            }
        }
        catch (Exception err)
        {
            utility.log(err.StackTrace);
        }
        return dt;
    }
    
    public DataTable getTypeAccBankGT()
    {
     
        DataTable dt = new DataTable("tipo");
        dt.Columns.Add("id");
        dt.Columns.Add("nombre");
  

        
       RE_GenericBean rg= new RE_GenericBean();

        for (int i= 0; i<4; i++){

            rg = new RE_GenericBean();
            switch (i){
            
                case 0:  rg.strC1 = "Select One";
                         rg.intC1 = 0;
                         break;
                case 1: rg.strC1 = "Ahorro";
                         rg.intC1 = 1;
                         break;
                case 2: rg.strC1 = "Monedero";
                         rg.intC1 = 2;
                         break;
                case 3: rg.strC1 = "Monetario";
                         rg.intC1 = 3;
                         break;
               
            
            }
            object[] objArr = { rg.intC1, rg.strC1 };
            dt.Rows.Add(objArr);
                
            
        }
        return dt;
      
    }
    protected void bt_edit_Click(object sender, EventArgs e)
    {
        if (Session["emp_id"] != null)
        {
            emp_id = (int)Session["emp_id"];
        }
        emp_id = int.Parse(lb_emp_id.Text);
        habilitocampos(true);

        if (emp_id == 0) { Button1.Enabled = false; }
        bt_save.Visible = true;
        bt_edit.Visible = false;
        if (TabContainer1.ActiveTabIndex == 5) { bt_upload_doc.Visible = true;  }
        
    }

    private void habilitocampos(bool p)
    {
        enable = p;
        tb_id.Enabled = p;
        tb_nombre.Enabled = p;
        tb_nombre2.Enabled = p;
        tb_apellido.Enabled = p;
        tb_apellido2.Enabled = p;
        tb_apellidocasada.Enabled = p;
        tb_irtra.Enabled = p;

        tb_add1.Enabled = p;
        tb_add2.Enabled = p;
        tb_base_salary.Enabled = p;
        tb_bonus.Enabled = p;
        tb_celular.Enabled = p;
        tb_city.Enabled = p;
        tb_codigoempresa.Enabled = p;
        tb_commision.Enabled = p;
        //ddl_country.Enabled = p;
     
        tb_email.Enabled = p;
      
        tb_emename_1.Enabled = p;
        tb_emename_2.Enabled = p;
        tb_emerelacion_1.Enabled = p;
        tb_emerelacion_2.Enabled = p;
        
        tb_emetel_1.Enabled = p;
    
        tb_emecel_1.Enabled = p;
        tb_emecel_2.Enabled = p;
        tb_emetel_3.Enabled = p;
       
        tb_end_contract.Enabled = p;
        tb_end_date.Enabled = p;
        ddl_dia.Enabled = p;
        ddl_anio.Enabled = p;
        ddl_mes.Enabled = p;
        
        tb_hired_date.Enabled = p;
        tb_igss.Enabled = p;
        tb_nit.Enabled = p;
        tb_other.Enabled = p;
        tb_personalemail.Enabled = p;
        tb_several_amount.Enabled = p;
        tb_start_contract.Enabled = p;
        tb_state.Enabled = p;
        tb_tel.Enabled = p;
        tb_zip.Enabled = p;
        tb_sevagree.Enabled = p;
        //ddl_company.Enabled = p;
        ddl_cuenta.Enabled = p;
        ddl_empoyement.Enabled = p;
        ddl_estadocivil.Enabled = p;
        ddl_genero.Enabled = p;
        ddl_jobtitle.Enabled = p;
        ddl_moneda.Enabled = p;
        ddl_status.Enabled = p;
        ddl_inmediate_superior.Enabled = p;
        ddl_type_id.Enabled = p;

        //fu_photo.Enabled = p;
        gv_bank.Enabled = p;
        Button1.Enabled = p;
       // bt_subir.Enabled = p;
        emp_id= int.Parse(lb_emp_id.Text);
       
       // gv_documents.Enabled = p;
        bt_upload_doc.Enabled = p;
        bt_upload_doc.Visible = p;
        lnk_add_doc.Visible = p;
        pnlAddDoc.Visible = p;
        Button1.Visible = p;
        Panel1.Visible = p;
        if (emp_id == 0)
        {
            Button1.Enabled = false;

        }
        enableDocuments(p);
        
        
        }

    private void enableDocuments(bool p)
    {
        if (gv_documents.Rows.Count > 0)
        {
            foreach (GridViewRow ROW in gv_documents.Rows)
            {
                int con = ROW.Controls.Count;
                DropDownList ddl = (DropDownList)ROW.FindControl("ddl_document");
                FileUpload fu = (FileUpload)ROW.FindControl("fu_document");
                ImageButton ln = (ImageButton)ROW.FindControl("ln_view");
                ddl.Enabled = p;
                fu.Enabled = p;
                ln.Enabled = true;


            }


        }
    }
    protected void bt_cancel_Click(object sender, EventArgs e)
    {
        string url = "Ams_general.aspx";
        if (Session["account"] != null)
        {
            url = "Ams_general.aspx?account=" + Session["account"].ToString();
        }
        else
        {
            if (ddl_cuenta.SelectedIndex != -1 || ddl_cuenta.SelectedIndex != 0)
            {
                url = "Ams_general.aspx?account=" + ddl_cuenta.SelectedValue.ToString();
            }

        }
        Response.Redirect(url);
    }
    protected void bt_delete_Click(object sender, EventArgs e)
    {
        if (Request.QueryString["emp_id"] != null) emp_id = int.Parse(Request.QueryString["emp_id"].ToString());
        
        if (emp_id != 0)
        {
           
            HyperLink1_ModalPopupExtender.Show();
           
        }
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
      //  int result = DB.DeleteEmergencyContact(int.Parse(lb_emergency_id_1.Text));
       int result2 = DB.DeleteContactInfo(int.Parse(lb_contactid_1.Text));
        //int result3 = DB.DeleteExpedienteInfo(int.Parse(lb_expediente_id_1.Text));
        int result4 = DB.DeletePersona(int.Parse(lb_person_id.Text));
        int result5 = DB.DeleteEmployee(int.Parse(lb_emp_id.Text));
        
        Response.Redirect("Ams_general.aspx");
    }
    protected void LinkButton3_Click(object sender, EventArgs e)
    {
        emp_id= (int)Session["emp_id"];
        Response.Redirect("PersonalInfo.aspx?emp_id="+emp_id);
    }
    protected void ddl_cuenta_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddl_cuenta.SelectedIndex != -1 && ddl_cuenta.SelectedIndex != 0)
        {
            ListItem item = null;
            ddl_inmediate_superior.Items.Clear();
            item = new ListItem("Select One", "Select One");
            ddl_inmediate_superior.Items.Add(item);
            if (ddl_cuenta.Items.Count > 1)
            {
                ArrayList accarr = null;

                try
                {
                    //utility.log("El valor de la cuenta es :  " + ddl_cuenta.SelectedValue.ToString());

                    int cuentaid = int.Parse(ddl_cuenta.SelectedValue);
                    //accarr = (ArrayList)DB.getEmployementArrbyCuenta(cuentaid);
                    emp_id = int.Parse(lb_emp_id.Text);
                    accarr = (ArrayList)DB.getSuperiors(emp_id);
                    if (accarr != null && accarr.Count > 0)
                    {
                        foreach (RE_GenericBean ac in accarr)
                        {
                            item = new ListItem(ac.strC1 + " " + ac.strC2, ac.intC1.ToString());
                            ddl_inmediate_superior.Items.Add(item);
                        }
                    }
                }
                catch (Exception err)
                {
                    utility.log(err.StackTrace + err.Message);
                }

            }
        }else{
            ddl_inmediate_superior.Items.Clear();
            
        
        }
    }


    protected void Button2_Click(object sender, EventArgs e)
    {

        lb_bankaccerror.Visible = false;

        emp_id = int.Parse(lb_emp_id.Text.ToString());
        if (ddl_bankpopup.SelectedIndex != -1 && ddl_bankpopup.SelectedIndex != 0 && emp_id != 0)
        {
            RE_GenericBean rg = new RE_GenericBean();
            rg.intC1 = emp_id;//emp_id
            rg.intC2 = int.Parse(ddl_bankpopup.SelectedValue);// bank
            rg.strC1 = tb_accpopup.Text;// acc#
            rg.strC2 = cb_typepopup.SelectedValue.ToString();//acc type
            rg.intC3 = 0;//accbank_id

            bool res = DB.InsertaCuentaBanco(rg, false);
            if (res)
            {
                if (emp_id != 0)
                {
                    ArrayList personabankacc = DB.getArrEmpleadoCuentaBanco(" where emp_accbank_empid=" + emp_id + "and emp_accbank_status=1");
                    if (personabankacc != null && personabankacc.Count > 0)
                    {
                        DataTable dt = utility.getDataTableEmpleadoBancoCuenta(personabankacc);
                        gv_bank.DataSource = dt;
                        gv_bank.DataBind();
                    }
                }
            }




        }
        else {
            lb_bankaccerror.Visible = true;
            lb_bankaccerror.Text= "Bank Account not saved. All Fields are Required ";
            Button1_ModalPopupExtender.Show();
        
        }

        Button1.Enabled = true;
        gv_bank.Enabled = true;
    }

    protected void editdetail(object sender, EventArgs e)
    {
        int index = 0;

        ImageButton bt = (ImageButton)sender;
        bt.Visible = false;
        GridViewRow objFila = (GridViewRow)bt.Parent.Parent;
        ImageButton btedit = (ImageButton)objFila.FindControl("bt_savedetail");
        btedit.Visible = true;

        index = objFila.RowIndex;
        int i = 0;




        foreach (GridViewRow ROW in gv_bank.Rows)
        {
            if (i == index)
            {
                int con = ROW.Controls.Count;
                TextBox tbacc = (TextBox)ROW.FindControl("tb_accnum");
                DropDownList ddlbank = (DropDownList)ROW.FindControl("ddl_bankdetail");
                DropDownList ddltype = (DropDownList)ROW.FindControl("ddl_typeacc");
                Label lbid = (Label)ROW.FindControl("lb_id");
                
                tbacc.Enabled = true;
                ddlbank.Enabled = true;
                ddltype.Enabled = true;
                
                

            }

            i++;
        }
    }
    protected void editdocument(object sender, EventArgs e)
    {
        int index = 0;

        ImageButton bt = (ImageButton)sender;
        bt.Visible = false;
        GridViewRow objFila = (GridViewRow)bt.Parent.Parent;
        ImageButton btedit = (ImageButton)objFila.FindControl("bt_savedocument");
        btedit.Visible = true;

        index = objFila.RowIndex;
        int i = 0;




        foreach (GridViewRow ROW in gv_bank.Rows)
        {
            if (i == index)
            {
                int con = ROW.Controls.Count;
                TextBox tbacc = (TextBox)ROW.FindControl("tb_accnum");
                DropDownList ddlbank = (DropDownList)ROW.FindControl("ddl_bankdetail");
                DropDownList ddltype = (DropDownList)ROW.FindControl("ddl_typeacc");
                Label lbid = (Label)ROW.FindControl("lb_id");

                tbacc.Enabled = true;
                ddlbank.Enabled = true;
                ddltype.Enabled = true;



            }

            i++;
        }
    }

    protected void savedetail(object sender, EventArgs e)
    {
        int index = 0;
        ImageButton bt = (ImageButton)sender;
        GridViewRow objFila = (GridViewRow)bt.Parent.Parent;
        ImageButton btedit = (ImageButton)objFila.FindControl("bt_editdocument");
        btedit.Visible = true;

        index = objFila.RowIndex;


        int i = 0;

      
        foreach (GridViewRow ROW in gv_bank.Rows)
        {
            if (i == index)
            {
                int con = ROW.Controls.Count;
              
                ImageButton bt2 = (ImageButton)ROW.FindControl("bt_savedetail");
                bt2.Visible = false;
                TextBox tbacc = (TextBox)ROW.FindControl("tb_accnum");
                DropDownList ddlbank = (DropDownList)ROW.FindControl("ddl_bankdetail");
                DropDownList ddltype = (DropDownList)ROW.FindControl("ddl_typeacc");
                Label lbid = (Label)ROW.FindControl("lb_id");
              

                RE_GenericBean rg = new RE_GenericBean();

                rg.intC1 = emp_id;//emp_id
                rg.intC2 = int.Parse(ddlbank.SelectedValue);// bank
                rg.strC1 = tbacc.Text;// acc#
                rg.strC2 = ddltype.SelectedValue.ToString();//acc type
                rg.intC3 = int.Parse(lbid.Text);//accbank_id

                bool res = DB.InsertaCuentaBanco(rg, true);

                if (res)
                {
                    if (emp_id != 0)
                    {
                        ArrayList personabankacc = DB.getArrEmpleadoCuentaBanco(" where emp_accbank_empid=" + emp_id + "and emp_accbank_status=1");
                        if (personabankacc != null && personabankacc.Count > 0)
                        {
                            DataTable dt = utility.getDataTableEmpleadoBancoCuenta(personabankacc);
                            gv_bank.DataSource = dt;
                            gv_bank.DataBind();
                        }
                    }
                    tbacc.Enabled = false;
                    ddlbank.Enabled = false;
                    ddltype.Enabled = false;
                }

            }

            i++;
        }

        

    }
    protected void savedocument(object sender, EventArgs e)
    {
        int index = 0;
        ImageButton bt = (ImageButton)sender;
        GridViewRow objFila = (GridViewRow)bt.Parent.Parent;
        ImageButton btedit = (ImageButton)objFila.FindControl("bt_editdetail");
        btedit.Visible = true;

        index = objFila.RowIndex;


        int i = 0;


        foreach (GridViewRow ROW in gv_bank.Rows)
        {
            if (i == index)
            {
                int con = ROW.Controls.Count;

                ImageButton bt2 = (ImageButton)ROW.FindControl("bt_savedetail");
                bt2.Visible = false;
                TextBox tbacc = (TextBox)ROW.FindControl("tb_accnum");
                DropDownList ddlbank = (DropDownList)ROW.FindControl("ddl_bankdetail");
                DropDownList ddltype = (DropDownList)ROW.FindControl("ddl_typeacc");
                Label lbid = (Label)ROW.FindControl("lb_id");


                RE_GenericBean rg = new RE_GenericBean();

                rg.intC1 = emp_id;//emp_id
                rg.intC2 = int.Parse(ddlbank.SelectedValue);// bank
                rg.strC1 = tbacc.Text;// acc#
                rg.strC2 = ddltype.SelectedValue.ToString();//acc type
                rg.intC3 = int.Parse(lbid.Text);//accbank_id

                bool res = DB.InsertaCuentaBanco(rg, true);

                if (res)
                {
                    if (emp_id != 0)
                    {
                        ArrayList personabankacc = DB.getArrEmpleadoCuentaBanco(" where emp_accbank_empid=" + emp_id + "and emp_accbank_status=1");
                        if (personabankacc != null && personabankacc.Count > 0)
                        {
                            DataTable dt = utility.getDataTableEmpleadoBancoCuenta(personabankacc);
                            gv_bank.DataSource = dt;
                            gv_bank.DataBind();
                        }
                    }
                    tbacc.Enabled = false;
                    ddlbank.Enabled = false;
                    ddltype.Enabled = false;
                }

            }

            i++;
        }



    }
    //Borra la cuenta bancaria asociada al empleado
    protected void deletedetail(object sender, EventArgs e)
    {
        //lb_error.Visible = false;
        int index = 0;
        ImageButton bt = (ImageButton)sender;
        GridViewRow objFila = (GridViewRow)bt.Parent.Parent;
       

        index = objFila.RowIndex;


        int i = 0;


        foreach (GridViewRow ROW in gv_bank.Rows)
        {
            if (i == index)
            {
                int con = ROW.Controls.Count;

                Label lbid = (Label)ROW.FindControl("lb_id");
                int id = int.Parse(lbid.Text);


                int res = DB.DeleteEmpleadoCuentaBanco(id);
                if (res == 1)
                {
                    if (emp_id != 0)
                    {
                        ArrayList personabankacc = DB.getArrEmpleadoCuentaBanco(" where emp_accbank_empid=" + emp_id + "and emp_accbank_status=1");
                        if (personabankacc != null && personabankacc.Count > 0)
                        {
                            DataTable dt = utility.getDataTableEmpleadoBancoCuenta(personabankacc);
                            gv_bank.DataSource = dt;
                            gv_bank.DataBind();
                        }

                    }
                }
                else { 
                  
                   // lb_error.Text = "Bank Account no Deleted";
                  //  lb_error.Visible = true;
                }

            }

            i++;
        }



    }
    protected void deletedocument(object sender, EventArgs e)
    {
        //lb_error.Visible = false;
        int index = 0;
        ImageButton bt = (ImageButton)sender;
        GridViewRow objFila = (GridViewRow)bt.Parent.Parent;


        index = objFila.RowIndex;


        int i = 0;


        foreach (GridViewRow ROW in gv_documents.Rows)
        {
            if (i == index)
            {
                int con = ROW.Controls.Count;

                Label lbid = (Label)ROW.FindControl("lb_iddocemp");
                Label lbidtipodoc = (Label)ROW.FindControl("lb_iddoc");
                int id = int.Parse(lbid.Text);
                int tempidtipodoc = DB.getIdNombreTipoDoc("Photo");

                int res = DB.DeleteDocumento(id);
                if (res == 1)
                {
                    if (lbidtipodoc.Text.Equals(tempidtipodoc.ToString())) 
                    {
                        photo.ImageUrl = "images/no-user-picture-set.png";
                    }
                    llenardocumentos();
                    enableDocuments(enable);
                }
                else
                {

                    //lb_error.Text = "Bank Account no Deleted";
                    //lb_error.Visible = true;
                }

            }

            i++;
        }



    }

    protected void bt_cancelpopup_Click(object sender, EventArgs e)
    {
        Button1_ModalPopupExtender.Hide();
        gv_bank.Enabled = true;
        Button1.Enabled = true;
    }
    protected void Button1_Click1(object sender, EventArgs e)
    {
        //lb_error.Visible = false;
        showErrors(false, 1);
        if (validar(1)){}

      
        else {
           // lb_error.Text = "Error. Personal Information not Save";
           // lb_error.Style.Add("color", "Red");
          //  lb_error.Visible = true;
       
        }
    }

    protected void bt_save_affiliation_Click(object sender, EventArgs e)
    {
        Guardar(2);
        
        
    }
    protected void bt_save_emergency_Click(object sender, EventArgs e)
    {
        Guardar(3);
    }
    protected void bt_save_salary_Click(object sender, EventArgs e)
    {
        Guardar(5);
    }
    
    protected void bt_save_job_Click(object sender, EventArgs e)
    {
        Guardar(4);
    }
    protected void ddl_estadocivil_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddl_genero.SelectedValue.Equals("F") && ddl_estadocivil.SelectedValue.Equals("MARRIED"))
        {

            tb_apellidocasada.Enabled = true;
        }
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
       // lb_error.Text = "Subiendo FOTO"+fu_photo.FileName;
        //lb_error.Style.Add("color", "Red");
        //lb_error.Visible = true;
    }
   /* protected void bt_subir_Click(object sender, EventArgs e)
    {
        lb_error.Visible = false;
        if (!lb_person_id.Text.Trim().Equals("0") && fu_photo.HasFile)
        {
            //guardando Photo
            string sphoto = "";
            if (fu_photo.PostedFile != null)
            {

                string filepath = fu_photo.FileName;
                if (filepath != "")
                {
                    string[] ext = filepath.Split('.');


                    fu_photo.PostedFile.SaveAs(Server.MapPath("~//data//photos//p_" + DateTime.Now.Year + DateTime.Now.Month + DateTime.Now.Day + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second + "." + ext[1]));
                    sphoto = "~/data/photos/p_" + DateTime.Now.Year + DateTime.Now.Month + DateTime.Now.Day + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second + "." + ext[1];
                    photo.ImageUrl = sphoto;
                    bool res =  DB.InsertFoto(sphoto, int.Parse(lb_person_id.Text.Trim()));

                }
            }


        }
        else {
            if (lb_person_id.Text.Trim().Equals("0"))
            {
                lb_error.Text = "First Save data Person";
            }
            if (!fu_photo.HasFile)
            {
                lb_error.Text = "Please choose a image";
            }
            lb_error.Style.Add("color", "Red");
            lb_error.Visible = true;
        
        }
    }
    * */
    protected void ddl_genero_SelectedIndexChanged(object sender, EventArgs e)
    {
        //lb_error.Text = "Cambio Genero";
    }
    protected void lnk_gua_Click(object sender, EventArgs e)
    {
        ddl_country.SelectedValue = "1";
        personalizarFormulario(1);
        ddl_moneda.SelectedValue = "GTQ";
        
    }
    protected void lnk_usa_Click(object sender, EventArgs e)
    {
        ddl_country.SelectedValue = "2";
        personalizarFormulario(2);
        ddl_moneda.SelectedValue = "USD";
    }
    protected void lnk_mx_Click(object sender, EventArgs e)
    {
        ddl_country.SelectedValue = "3";
        personalizarFormulario(3);
        ddl_moneda.SelectedValue = "USD";
    }
    protected void ddl_country_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddl_type_id.SelectedIndex = 0;
        personalizarFormulario(int.Parse(ddl_country.SelectedValue));
    }
    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        LinkButton4_ModalPopupExtender.Hide();
        string url = Request.UrlReferrer.ToString();
        if ( Session["history"]!=null){
            url = Session["history"].ToString();
        }
            Response.Redirect(url);
        
      
    }
    protected void bt_save_doc_Click(object sender, EventArgs e)
    {
        Response.Redirect("Ams_general.aspx");
        
      
       

    }
    protected void bt_save_Click(object sender, EventArgs e)
    {
        int res = 1;
       /* if (TabPanel1.Enabled == true && TabContainer1.ActiveTabIndex==0) */ res= res * Guardar(1);
        /*if (TabPanel2.Enabled == true && TabContainer1.ActiveTabIndex == 1) */res = res * Guardar(2);
        /*if (TabPanel3.Enabled == true && TabContainer1.ActiveTabIndex == 2)*/ res = res * Guardar(3);
        /*if (TabPanel4.Enabled == true && TabContainer1.ActiveTabIndex == 3)*/ res = res * Guardar(4);
        /*if (TabPanel5.Enabled == true && TabContainer1.ActiveTabIndex == 4)*/ res = res * Guardar(5);
       /* if (TabPanel6.Enabled == true && TabContainer1.ActiveTabIndex == 5) {*/ res = res * 1; validarTabs(); //}
        if (res == 1)
        {
            bt_save.Visible = false;
            //bt_cancel.Visible = true;
            bt_edit.Visible = true;
            habilitocampos(false);
        }
       
       
    }
    protected void gv_documents_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow) {
           
            Label label= (Label)e.Row.FindControl("lb_obligatorio");
            if (label.Text.Equals("1")) { label.Text = "*"; label.ForeColor = System.Drawing.Color.Red; }
            else { label.Text = "-"; label.ForeColor = System.Drawing.Color.White; }

            Label label2 = (Label)e.Row.FindControl("lb_path");
            if (label2 != null && !label2.Text.Equals(""))
            {
                
                ImageButton imagen = (ImageButton)e.Row.FindControl("ln_view");
                imagen.OnClientClick = "javascript:window.open('../data/documents/" + label2.Text + "');";
                imagen.Visible = true;
            }
        }
    }
    protected void bt_upload_doc_Click(object sender, EventArgs e)
    {
        if (bt_save_doc.Visible==true && bt_save_doc.Enabled== true)  bt_save_doc.Enabled = false;
        foreach (GridViewRow ROW in gv_documents.Rows)
        {

            FileUpload fu = (FileUpload)ROW.FindControl("fu_document");

            if (fu!= null && fu.PostedFile!= null)
            {
                 string filepath = fu.FileName;
                if (filepath != "")
                {
                    string[] ext = filepath.Split('.');
                    DropDownList ddlphoto = (DropDownList)ROW.FindControl("ddl_document");
                   
                    Label lbid = (Label)ROW.FindControl("lb_iddoc");
                    Label lbiddoc = (Label)ROW.FindControl("lb_iddocemp");
                    RE_GenericBean re = new RE_GenericBean();
                    ImageButton hy = (ImageButton)ROW.FindControl("ln_view");
                    re.intC1 = int.Parse(lb_emp_id.Text);
                    re.intC2 = int.Parse(lbid.Text);
                    re.intC5 = int.Parse(lbiddoc.Text);
                    
                    bool actualiza= false;
                    if (re.intC5 != 0) actualiza = true;  
                    RE_GenericBean res = DB.InsertaDocumento(re,actualiza);
                    if (res.intC1 == 1 && res.intC2!= 0) {
                        string horatemp = DateTime.Now.Year.ToString()+DateTime.Now.Month.ToString()+DateTime.Now.Day.ToString() + DateTime.Now.Hour+DateTime.Now.Minute.ToString();
                        re.intC5 = res.intC2; 
                        fu.PostedFile.SaveAs(Server.MapPath("~//data//documents//doc_" + re.intC5 + "_"+lbid.Text+"_user_" + re.intC1 +"_"+horatemp+ "." + ext[1]));
                        re.strC1 = "doc_" + re.intC5 + "_"+lbid.Text+"_user_" + re.intC1 + "_"+horatemp+"." + ext[1];
                        hy.OnClientClick = "javascript:window.open('../data/documents/" + re.strC1 + "');";
                        RE_GenericBean res2 = DB.InsertaDocumento(re, true);
                        if (res2.intC1 == 1 && res2.intC2 != 0)
                        {

                            if (ddlphoto.SelectedItem.ToString().ToUpper().Equals("PHOTO"))
                            {
                                photo.ImageUrl = "~/data/documents/" + re.strC1;
                            }

                          
                            
                        }
                    }
                }

               
                
               

                
            }
        }
        if (bt_save.Visible)
        {
            bt_save.Visible = false;
            bt_edit.Visible = true;
            bt_upload_doc.Visible = false;
            habilitocampos(false);
        }
        if (bt_save_doc.Visible)
        {
           // bt_save_doc.Visible = false;
            bt_edit_doc.Visible = true;
            bt_upload_doc.Visible = false;
            habilitocampos(false);

        }
        llenardocumentos();
        habilitocampos(enable);
        validarTabs();
        if (bt_save_doc.Visible == true ) bt_save_doc.Enabled = true;
    }

    protected void lnk_cancel_newdoc_Click(object sender, EventArgs e)
    {
        lnk_add_doc_ModalPopupExtender.Hide();
    }
    protected void lnk_addnewdoc_Click(object sender, EventArgs e)
    {
        if (fu_newdoc != null && fu_newdoc.PostedFile != null)
        {
            string filepath = fu_newdoc.FileName;
            if (filepath != "")
            {
                string[] ext = filepath.Split('.');
                RE_GenericBean re = new RE_GenericBean();
                re.intC1 = int.Parse(lb_emp_id.Text);
                re.intC2 = int.Parse(ddl_newdoc.SelectedValue.ToString());
                re.intC5 = 0;

                bool actualiza = false;
                if (re.intC5 != 0) actualiza = true;
                RE_GenericBean res = DB.InsertaDocumento(re, actualiza);
                if (res.intC1 == 1 && res.intC2 != 0)
                {
                    re.intC5 = res.intC2;
                    string horatemp = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour + DateTime.Now.Minute.ToString();
                    fu_newdoc.PostedFile.SaveAs(Server.MapPath("~//data//documents//doc_" + re.intC5 + "_" + re.intC2 + "_user_" + re.intC1 + horatemp+"." + ext[1]));
                    re.strC1 = "doc_" + re.intC5 + "_" + re.intC2 + "_user_" + re.intC1 +horatemp+ "." + ext[1];
                    
                    RE_GenericBean res2 = DB.InsertaDocumento(re, true);
                    if (res2.intC1 == 1 && res2.intC2 != 0)
                    {
                        lnk_add_doc_ModalPopupExtender.Hide();
                    }
                    else { 
                    
                    
                    }

                }
            }
            llenardocumentos();
            validarTabs();
        }
    }

    protected void ln_back_Click(object sender, EventArgs e)
    {
       
        string url = Request.UrlReferrer.ToString();
        if (Session["account"] != null)
        {
            url = "Ams_general.aspx?account=" + Session["account"].ToString();
        }
        else {
            if (ddl_cuenta.SelectedIndex != -1 || ddl_cuenta.SelectedIndex != 0)
            {
                url = "Ams_general.aspx?account=" + ddl_cuenta.SelectedValue.ToString();
            }
        
        }
        Response.Redirect(url);
    }
    protected void bt_cancel_salary_Click(object sender, EventArgs e)
    {

    }
}

 

               


                    